﻿function showCompareUser(usernameA, usernameB) {
	if (refreshWin('showCompareUser_' + usernameA + '_' + usernameB)) return;
	getUserCompare({
				usernameA : usernameA,
				usernameB : usernameB,
				success : onload
			});

	function onload(data) {
		var acstore_reader = new Ext.data.JsonReader({
					id : 'rid'
				}, ['rid', 'pid', 'problemTitle', 'accept', 'submit', 'exeTime', 'exeMem', 'lang',
						'langCode', 'codeLen', 'allowCode']);
		var onlyaac_store = new Ext.data.Store({
					autoLoad : true,
					proxy : new Ext.data.MemoryProxy(data.onlyAAccept),
					reader : acstore_reader
				});
		var onlybac_store = new Ext.data.Store({
					autoLoad : true,
					proxy : new Ext.data.MemoryProxy(data.onlyBAccept),
					reader : acstore_reader
				});
		var bothac_store = new Ext.data.Store({
					autoLoad : true,
					proxy : new Ext.data.MemoryProxy(data.bothAccept),
					reader : new Ext.data.JsonReader({
								id : 'rid'
							},
							['winner', 'rid', 'pid', 'problemTitle', 'accept', 'submit', 'exeTime',
									'exeMem', 'lang', 'langCode', 'codeLen', 'allowCode'])
				});

		var failstore_reader = new Ext.data.JsonReader({
					id : 'pid'
				}, ['pid', 'problemTitle', 'submit']);
		var onlyafail_store = new Ext.data.Store({
					autoLoad : true,
					proxy : new Ext.data.MemoryProxy(data.onlyAFail),
					reader : failstore_reader
				});
		var onlybfail_store = new Ext.data.Store({
					autoLoad : true,
					proxy : new Ext.data.MemoryProxy(data.onlyBFail),
					reader : failstore_reader
				});
		var bothfail_store = new Ext.data.Store({
					autoLoad : true,
					proxy : new Ext.data.MemoryProxy(data.bothFail),
					reader : new Ext.data.JsonReader({
								id : 'pid'
							}, ['pid', 'problemTitle', 'submitA', 'submitB'])
				});

		var solve_cm = new Ext.grid.ColumnModel([{
					header : 'PID',
					sortable : false,
					dataIndex : 'pid',
					renderer : sys.rd.pid
				}, {
					header : 'Submitions',
					sortable : false,
					dataIndex : 'submit',
					renderer : sys.rd.acsubmit
				}, {
					header : 'ExeTime',
					sortable : false,
					dataIndex : 'exeTime',
					renderer : sys.rd.exetime
				}, {
					header : 'ExeMemory',
					sortable : false,
					dataIndex : 'exeMem',
					renderer : sys.rd.exemem
				}, {
					header : 'CodeLength',
					sortable : false,
					dataIndex : 'codeLen',
					renderer : sys.rd.codelen
				}, {
					header : 'Language',
					sortable : false,
					dataIndex : 'lang'
				}]);
		var bothsolve_cm = new Ext.grid.ColumnModel([{
			header : 'Winner',
			sortable : false,
			dataIndex : 'winner',
			renderer : function(value, p, record) {
				return String
						.format(
								'<a href="javascript:void(0);" onclick="showUser(\'{0}\');" title="{1}" class="likelink">{0}</a>',
								value, value == data.usernameA ? getauthor(data.usernameA,
										data.nicknameA) : getauthor(data.usernameB, data.nicknameB));
			}
		}, {
			header : 'PID',
			sortable : false,
			dataIndex : 'pid',
			renderer : sys.rd.pid
		}, {
			header : 'Submitions',
			sortable : false,
			dataIndex : 'submit',
			renderer : sys.rd.acsubmit
		}, {
			header : 'ExeTime',
			sortable : false,
			dataIndex : 'exeTime',
			renderer : sys.rd.exetime
		}, {
			header : 'ExeMemory',
			sortable : false,
			dataIndex : 'exeMem',
			renderer : sys.rd.exemem
		}, {
			header : 'CodeLength',
			sortable : false,
			dataIndex : 'codeLen',
			renderer : sys.rd.codelen
		}, {
			header : 'Language',
			sortable : false,
			dataIndex : 'lang'
		}]);
		var fail_cm = new Ext.grid.ColumnModel([{
					header : 'PID',
					sortable : false,
					dataIndex : 'pid',
					renderer : sys.rd.pid
				}, {
					header : 'Submitions',
					sortable : false,
					dataIndex : 'submit'
				}]);
		var bothfail_cm = new Ext.grid.ColumnModel([{
					header : 'PID',
					sortable : false,
					dataIndex : 'pid',
					renderer : sys.rd.pid
				}, {
					header : 'ASubmit',
					sortable : false,
					dataIndex : 'submitA'
				}, {
					header : 'BSubmit',
					sortable : false,
					dataIndex : 'submitB'
				}]);

		var onlyaac_grid = new Ext.grid.GridPanel({
					title : '<center>只有 ' + data.usernameA + ' 通过</center>',
					region : 'center',
					anchor : '100% 35%',
					loadMask : {
						msg : '载入中...'
					},
					viewConfig : {
						forceFit : true
					},
					store : onlyaac_store,
					cm : solve_cm
				});
		var onlybac_grid = new Ext.grid.GridPanel({
					title : '<center>只有 ' + data.usernameB + ' 通过</center>',
					region : 'center',
					anchor : '100% 35%',
					loadMask : {
						msg : '载入中...'
					},
					viewConfig : {
						forceFit : true
					},
					store : onlybac_store,
					cm : solve_cm
				});
		var bothac_grid = new Ext.grid.GridPanel({
					title : '<center>两者都通过</center>',
					region : 'center',
					anchor : '100% 30%',
					loadMask : {
						msg : '载入中...'
					},
					viewConfig : {
						forceFit : true
					},
					store : bothac_store,
					cm : bothsolve_cm
				});
		var onlyafail_grid = new Ext.grid.GridPanel({
					title : '<center>只有 ' + data.usernameA + ' 尝试失败</center>',
					region : 'east',
					anchor : '100% 35%',
					split : true,
					width : 200,
					loadMask : {
						msg : '载入中...'
					},
					viewConfig : {
						forceFit : true
					},
					store : onlyafail_store,
					cm : fail_cm
				});
		var onlybfail_grid = new Ext.grid.GridPanel({
					title : '<center>只有 ' + data.usernameB + ' 尝试失败</center>',
					region : 'east',
					anchor : '100% 35%',
					split : true,
					width : 200,
					loadMask : {
						msg : '载入中...'
					},
					viewConfig : {
						forceFit : true
					},
					store : onlybfail_store,
					cm : fail_cm
				});
		var bothfail_grid = new Ext.grid.GridPanel({
					title : '<center>两者都尝试失败</center>',
					region : 'east',
					anchor : '100% 30%',
					split : true,
					width : 200,
					loadMask : {
						msg : '载入中...'
					},
					viewConfig : {
						forceFit : true
					},
					store : bothfail_store,
					cm : bothfail_cm
				});
		var toolbar = new Ext.Toolbar({
					items : [new Ext.Button({
										text : '比较其他人',
										iconCls : 'compare-icon',
										handler : function() {
											compareUser();
											win.close();
										}
									}), new Ext.Toolbar.Separator(), new Ext.Button({
										text : data.usernameA + ' 的统计',
										iconCls : 'statistics-icon',
										handler : function() {
											showUserStatistics(data.usernameA);
											win.close();
										}
									}), new Ext.Button({
										text : data.usernameB + ' 的统计',
										iconCls : 'statistics-icon',
										handler : function() {
											showUserStatistics(data.usernameB);
											win.close();
										}
									}), new Ext.Toolbar.Fill(), new Ext.Button({
										text : '关闭',
										iconCls : 'ok-icon',
										handler : function() {
											win.close();
										}
									})]
				});
		var win = new Ext.Window({
					id : 'showCompareUser_' + data.usernameA + '_' + data.usernameB,
					title : '用户比较 - ' + data.usernameA + ' (VS) ' + data.usernameB,
					iconCls : 'compare-icon',
					layout : 'border',					width : 700,
					height : 500,
					minWidth : 600,
					minHeight : 400,
					items : [{
								region : 'center',
								layout : 'anchor',
								border : false,
								items : [onlyaac_grid, onlybac_grid, bothac_grid]
							}, {
								region : 'east',
								layout : 'anchor',
								width : '200',
								border : false,
								split : true,
								items : [onlyafail_grid, onlybfail_grid, bothfail_grid]
							}],
					bbar : toolbar
				});
		win.show();
	}
}

function getUserCompare(obj) {
	ajax({
				url : url.getUserCompare,
				params : {
					usernameA : obj.usernameA,
					usernameB : obj.usernameB
				},
				success : obj.success
			});
}
